import { isObject, useDateFormat, useClipboard } from '@vueuse/core'
import wx from 'weixin-js-sdk'

export const getImageUrl = (folder, image, suffix = 'svg') => {
  // image为相对路径，第二个参数:当前路径的URL
  return new URL(`../assets/images/${folder}-images/${image}.${suffix}`, import.meta.url).href
}

export const noSideSpace = value => !value.startsWith(' ') && !value.endsWith(' ')

export const timeFormat = time => {
  return useDateFormat(+new Date(time), 'YYYY-MM-DD HH:mm:ss').value
}

export const copyText = text => {
  const textarea = document.createElement('textarea')
  textarea.value = text
  document.body.appendChild(textarea)
  textarea.select()
  try {
    document.execCommand('copy')
    $message.success('复制成功', { duration: 1000 })
  } catch (error) {
    $message.error('您的浏览器不支持自动复制，请重试或手动复制', { duration: 2000 })
  }
  document.body.removeChild(textarea)
  // const { copied, isSupported, copy } = useClipboard();
  // if (isSupported) {
  //   copy(value);
  //   if (copied) {
  //     $message.success('复制成功', { duration: 1000 });
  //   }
  // } else {
  //   $message.error('您的浏览器不支持自动复制，请重试或手动复制', { duration: 2000 });
  // }
}

export const sleep = ms => {
  return new Promise(resolve => setTimeout(resolve, ms))
}

export const deepMerge = (src, target) => {
  let key
  for (key in target) {
    src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key])
  }
  return src
}

export const isWeiXin = () => {
  // 获取访问终端的版本信息,如果终端信息中存在微信标识' micromessenger ',则是微信
  const ua = window.navigator.userAgent.toLowerCase()
  if (ua.match(/MicroMessenger/i) == 'micromessenger') {
    return true
  }
  return false
}

export const isMiniProgram = () => {
  return new Promise(resolve => {
    if (!isWeiXin()) {
      resolve(false)
    } else {
      wx.miniProgram.getEnv(res => {
        console.log(res)
        resolve(res.miniprogram)
      })
    }
  })
}

export const MiniProgramJump = data => {
  wx.miniProgram.navigateTo(data)
}

// 检测设备ua
const os = (function () {
  const ua = navigator.userAgent
  const isWindowsPhone = /(?:Windows Phone)/.test(ua)
  const isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone
  const isAndroid = /(?:Android)/.test(ua)
  const isFireFox = /(?:Firefox)/.test(ua)
  // const isChrome = /(?:Chrome|CriOS)/.test(ua);
  const isTablet =
    /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua))
  const isPhone = /(?:iPhone)/.test(ua) && !isTablet
  const isPc = !isPhone && !isAndroid && !isSymbian
  return {
    isTablet,
    isPhone,
    isAndroid,
    isPc,
  }
})()

export const isMobile = () => {
  const flag = navigator.userAgent.match(
    /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
  )
  // 判断是否移动设备
  if (os.isAndroid || os.isPhone || flag) {
    return true
  }
  return false
}

export const getWechatVertion = () => {
  let version = ''
  if (isWeiXin()) {
    const wechatInfo = navigator.userAgent.match(/MicroMessenger\/([\d\.]+)/i)
    if (wechatInfo && wechatInfo.length > 1) {
      // 获取微信的版本号
      version = wechatInfo[1]
    }
  }
  return version
}

export const createId = () => {
  const secret = `${Math.random().toString(16).slice(-8)}${Math.random().toString(16).slice(-8)}`
  return secret
}
